package day15;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.junit.Test;

public class ClassTest {
	@Test
	public void test() throws ClassNotFoundException{
		Class clazz3 = Class.forName("day15.Person");
		Class clazz2 = Person.class;
		Person person = new Person();
		Class clazz1 = person.getClass();
		System.out.println(clazz1 == clazz2);
		System.out.println(clazz1 == clazz3);
	}
	
	@Test
	public void test2(){
		Class clazz1 = Person.class;
		Constructor[] constructors = clazz1.getDeclaredConstructors();
		for (Constructor constructor : constructors) {
			System.out.println(constructor);
			System.out.println(constructor.getName());
			System.out.println(constructor.getParameterTypes());
			System.out.println(constructor.getModifiers());
			System.out.println(constructor.getParameterCount());
		}
	}
	
	@Test
	public void test3() throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		Class clazz = Person.class;
		Constructor constructor = clazz.getConstructor(String.class,Integer.class,String.class);
		System.out.println(constructor);
		Person person = (Person)constructor.newInstance(new Object[]{"张三",20,"java"});
		System.out.println(person);
	}
	
	@Test
	public void test4() throws Exception{
		Class clazz = Class.forName("day15.Person");
		Constructor constructor = clazz.getConstructor();
		Person person = (Person)constructor.newInstance();
		Method method = clazz.getMethod("setName", String.class);
		method.invoke(person, "张三");
	}
	
	
}
